<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Slidestream
 * @category   Model
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */
class Slidestream extends Ot_Db_Table
{
    /**
     * Name of the table in the database
     *
     * @var string
     */
    protected $_name = 'tbl_slidestream';
  
    /**
     * Primary key of the table
     *
     * @var string
     */
    protected $_primary = 'slidestreamId';
    
    /**
     * Gets all the slide streams for an account
     */
    public function getSlidestreamsForAccount($accountId)
    {
        $where = $this->getAdapter()->quoteInto('accountId = ?', $accountId);
        return $this->fetchAll($where, 'title');
    }
    
    /**
     * Deletes a slide stream and all it's associated slides and mappings
     */
    public function deleteSlidestream($slidestreamId)
    {
        $dba = $this->getAdapter();
        
        $dba->beginTransaction();
        
        $where = $dba->quoteInto('slidestreamId = ?', $slidestreamId);
        
        try {
            $this->delete($where);
        } catch (Exception $e) {
            $dba->rollBack();
            throw $e;
        }
        
        // delete slide mappings
        $slidestreamMap = new Slidestream_Slide();
               
        try {
            $slidestreamMap->delete($where);
        } catch (Exception $e) {
            $dba->rollBack();
            throw $e;
        }
        
        // unassign devices that are assigned to the slidestream
        $slidestreamDevice = new Slidestream_Device();
            
        $where = $slidestreamDevice->getAdapter()->quoteInto('slidestreamId = ?', $slidestreamId);
            
        try {
            $slidestreamDevice->delete($where);
        } catch (Exception $e) {
            $dba->rollBack();
            throw $e;
        }
        
        $dba->commit();
    }
    
    /**
     * Generates the form for adding and editing a slide
     * 
     * @param array $values The values the form should be populated with
     * @return Zend_Form
     */
    public function form ($values = array())
    {
        $form = new Zend_Form();
        $form->setAttrib('id', 'slidestreamForm')
             ->setDecorators(array(
                     'FormElements',
                     array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form')),
                     'Form',
             ));
                
        // Create and configure element for the title of the slide:
        $title = $form->createElement('text', 'title', array('label' => 'Title:'));
        $title->setRequired(true)
              ->addFilter('StringTrim')
              ->addFilter('StripTags')
              ->setAttrib('maxlength', 255)
              ->setValue((isset($values['title'])) ? $values['title'] : '')
              ->addValidator('NotEmpty', true, array('messages' => array('isEmpty' => 'Must not be blank!')));
            
        $submit = $form->createElement('submit', 'submitButton', array('label' => 'Submit'));
        $submit->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formSubmit'))
                 ));

        $cancel = $form->createElement('button', 'cancel', array('label' => 'Cancel'));
        $cancel->setAttrib('id', 'cancel');
        $cancel->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formButton'))
                ));
        
        $form->addElements(array($title));

        $form->setElementDecorators(array(
                  'ViewHelper',
                  'Errors',
                  array('HtmlTag', array('tag' => 'div', 'class' => 'elm')),
                  array('Label', array('tag' => 'span')),
              ))
             ->addElements(array($submit, $cancel));

        if (isset($values['slidestreamId'])) {

            $slidestreamId = $form->createElement('hidden', 'slidestreamId');
            $slidestreamId->setValue($values['slidestreamId']);
            $slidestreamId->setDecorators(array(
                array('ViewHelper', array('helper' => 'formHidden'))
            ));

            $form->addElement($slidestreamId);
        }
        return $form;
    }
}